package com.zslibrary.BusMaster;

import android.util.Log;

import com.zslibrary.Enum.BoardId;
import com.zslibrary.Enum.ComponentClassDefine;
import com.zslibrary.Enum.Protocol;
import com.zslibrary.Pipe.Pipe;
import com.zslibrary.Pipe.StreamPipe;
import com.zslibrary.Protocol.ComponentTest;
import com.zslibrary.Structure.ErrorStruct;
import com.zslibrary.Structure.MessageStruct;
import com.zslibrary.Tools.FrameBuilder;
import com.zslibrary.Tools.ZSLog;

public class BusMicroControlUnit {
    private static StreamPipe pipe;
    private static BusMicroControlUnit mInstance;
//    private static BusBase bus;
    private static Pipe mPipeLine;

    public void startMCUBus(){
        pipe.setAsyncMessageHandler(new StreamPipe.AsyncMessageHandler() {
            @Override
            public void handleAsyncMessages(MessageStruct[] messages) {
                for(MessageStruct msg:messages){
                    if(msg==null)
                        break;
                    switch(msg.messageId){
                        case Protocol.MSG_SET_COMP_ERR:{
                            FrameBuilder msgBuilder=new FrameBuilder(msg.messageData);
                            byte[] err_bytes;
                            err_bytes=msgBuilder.popBytes(msg.messageLength-4);
                            short component=(short)msgBuilder.popData(2);
                            Boolean isFatal=((msgBuilder.popData(1)==1)?true:false);
                            Boolean isPersistent=((msgBuilder.popData(1))==1?true:false);
                            ErrorStruct error=new ErrorStruct(new String(err_bytes),isFatal,isPersistent);
                            HandleError.setError(error);
                            //ZSLog.d("report error:"+error.errorCode);
                        }break;

                        case Protocol.MSG_CLEAR_COMP_ERR:{
                            FrameBuilder msgBuilder=new FrameBuilder(msg.messageData);
                            byte[] err_bytes;
                            err_bytes=msgBuilder.popBytes(msg.messageLength-2);
                            HandleError.clearError(new String(err_bytes));
                        }break;

                        case Protocol.MSG_BOARD_STATUS:{
                        }break;

                        default:
                            pipe.putAsyncMessage(msg);
                            break;
                    }
                }
            }
        });

        if(pipe.launchPipe("/dev/ttyS1", BoardId.LOCAL_MCU,9600)){
//            bus=new BusBase();
        }
    }

    public static BusMicroControlUnit getInstance(){
        if(mInstance!=null)
            return mInstance;
        mInstance=new BusMicroControlUnit();
        pipe=new StreamPipe();
        return mInstance;
    }

    public Boolean controlLed(int color){
//        return BusComponentTest.componentTest(pipe,(short)101,(short)color);
//        ComponentTest.componentTest(mPipeLine,(short)101,(short)color);
        return true;
    }

    public Boolean openDoor(){
//        ComponentTest.componentTest(mPipeLine,(short)(ComponentClassDefine.C_AUXILIARY+1),(short)0);
        return true;
    }

    public Boolean closeDoor(){
//        ComponentTest.componentTest(mPipeLine,(short)(ComponentClassDefine.C_AUXILIARY+1),(short)1);
        return true;
    }

    public Boolean isDoorOpen(){
        int result= BusGetSensorValue.getSensorValue(pipe,(short)(ComponentClassDefine.C_AUXILIARY+1),0);
        if(result==1)
            return true;
        return false;
    }
}
